import Mock from 'mockjs'

//模拟 已注册的用户
var userList = [
    {
        "id": 1,
        "username": 'admin',
        "password": '123456',
        "name": '李佳奇',
        "leader": '马云',
        "pic": 'https://dummyimage.com/50x50/0ff'
    },
    {
        "id": 2,
        "username": 'user',
        "password": '1234',
        "name": '李小琪',
        "leader": '马化腾',
        "pic": 'https://dummyimage.com/50x50/ff0'
    }
]

//定义登陆接口
Mock.mock('/api/login', 'post', function (options) {
    //解析参数
    var params = JSON.parse(options.body);
    var { username, password } = params;

    var user = userList.find((item) => { return item.username === username && item.password === password });

    if ( user )
    {
        return { code: 200, msg: '登陆成功!',token: Mock.Random.word(), id: user.id  };    
    }
    else{
        return { code: 400, msg: '登陆失败!' };    
    }
})

//定义 获取用户信息 接口
Mock.mock('/api/user/info', 'post', function (options) {
    //解析参数
    var params = JSON.parse(options.body);
    var { id } = params;

    //根据用户 id 查找用户信息,返回
    var user = userList.find((item) => { return item.id === id });

    return { code: 200, info: user};    
    
})